package linkedList;

/**
 * @author leiyuee
 * @ClassName linkedList.DoubleLinkedListDemo 双向链表
 * @date 2021/11/5 16:37
 */
public class DoubleLinkedListDemo {
}


class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "linkedList.HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}


class DoubleLinkedList{
    private  HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }
    //遍历双向链表
    public void list(){
        if (head.next ==null){
            System.out.println("链表为空");
            return;
        }

        HeroNode2 temp = head.next;

        while (true){
            if (temp==null){
                break;
            }

            System.out.println(temp);
            temp=temp.next;
        }
    }
    //添加到最后
    public void add(HeroNode2 heroNode){
        HeroNode2 temp = head;
        while (true){
            if (temp.next ==null){
                break;
            }
            //不是最后就将temp后移
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    public void update(HeroNode2 newHeroNode) {
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;

        boolean flag = false;
        while (true){
            if (temp ==null){
                break;
            }
            if (newHeroNode.no == temp.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else {
            System.out.println("没有找到，不能修改");
        }


    }

    //删除
    public void  del(int no){
        if (head.next ==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;

        boolean flag = false;
        while (true){
            if (temp ==null){
                break;
            }
            if ( temp.no ==no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.pre.next = temp.next;
            if (temp.next !=null){
                temp.next.pre =temp.pre;
            }
        }else {

            System.out.println("删除节点未找到");
        }

    }
}
